//
//  QueueList.c
//  DataStruct
//
//  Created by vase on 2022/3/22.
//

#include "QueueList.h"

/** 链式存储队列的初始化函数*/
QueueList* initQueueList(void) {
    QueueList* queue = (QueueList *)malloc(sizeof(QueueList));
    /** 方便起见，此处存储一个头节点*/
    QueueListItem *ihead = (QueueListItem *)malloc(sizeof(QueueListItem));
    ihead->next = NULL;
    queue->head = queue->tail = ihead;
    return queue;
}

/** 队列入队*/
void pushQueueList(QueueList *queue, int item) {
    //新建一个节点
    QueueListItem *one = (QueueListItem *)malloc(sizeof(QueueListItem));
    one->value = item;
    one->next = NULL;
    //缀到尾巴上
    queue->tail->next = one;
    queue->tail = one;
}

/** 队列出队*/
int popQueueList(QueueList *queue) {
    if (queue->head == queue->tail) { //队列为空
        return -1;
    }else {
        //取出元素，头指针后移
        int result = queue->head->next->value;
        queue->head->next = queue->head->next->next;
        //如果取出的元素是最后一个元素，则取出后队列为空
        if (queue->head->next == NULL) {
            queue->tail = queue->head;
        }
        return result;
    }
}

/** 检验队列是否为空*/
int isEmptyQueueList(QueueList *queue) {
    if (queue->head == queue->tail) {
        return 1; //队列为空
    }else {
        return 0; //队列不为空
    }
}
